﻿using ProjectCommonLibs.models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectCommonLibs.utils
{
    public class DataDeSeirializeUtils
    {
        public static T deserializeData<T>(byte[] data) where T : IDataModel,new()
        {
            var variables = typeof(T).GetProperties();
            T t = new T();
            int startIndex = 3;
            foreach (var item in variables)
            {
                if (item.PropertyType == typeof(ushort))
                {
                    item.SetValue(t, MathUtils.getShort(data, startIndex));
                    startIndex += 2;
                }
                else if (item.PropertyType == typeof(short))
                {
                    item.SetValue(t, MathUtils.getSignedShort(data, startIndex));
                    startIndex += 2;
                }
                else if (item.PropertyType == typeof(int))
                {
                    item.SetValue(t, MathUtils.getSignedInt(data, startIndex));
                    startIndex += 4;
                }
                else if (item.PropertyType == typeof(uint))
                {
                    item.SetValue(t, MathUtils.getInt(data, startIndex));
                    startIndex += 4;
                }
                else
                {

                }
            }
            return t;
        }
        public static DTUAmmeterModel deserializeDTUAmmeterData(byte[] data)
        {
            int startIndex = 3;
            DTUAmmeterModel model = new DTUAmmeterModel();
            List<long> dtuData = new List<long>();
            for (int i = 0; i < 6; i++)
            {
                dtuData.Add(MathUtils.getShort(data, startIndex));
                startIndex += 2;
            }
            for (int i = 0; i < 9; i++)
            {
                dtuData.Add(MathUtils.getSignedShort(data, startIndex));
                startIndex += 2;
            }
            dtuData.Add(MathUtils.getShort(data, startIndex));
            startIndex += 2;
            for (int i = 0; i < 4; i++)
            {
                dtuData.Add(MathUtils.getInt(data, startIndex));
                startIndex += 2;
            }
            for (int i = 0; i < 3; i++)
            {
                dtuData.Add(MathUtils.getSignedShort(data, startIndex));
                startIndex += 2;
            }
            for (int i = 0; i < 4; i++)
            {
                dtuData.Add(MathUtils.getShort(data, startIndex));
                startIndex += 2;
            }
            model.data = dtuData;
            return model;
        }
        public static List<long> deserializeDTUAmmeter2Data(byte[] data)
        {
            List<long> model = new List<long>();
            int startIndex = 3;
            for (int i = 0; i < 29; i++)
            {
                model.Add(MathUtils.getSignedShort(data, startIndex));
                startIndex += 2;
            }
            for (int i = 0; i < 4; i++)
            {
                model.Add(MathUtils.getSignedInt(data, startIndex));
                startIndex += 4;
            }
            return model;
        }
    }
}
